// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Funky Funky Player
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 1 : TYPE_PLAYEROBJECT
public alias 256 : GROUP_PLAYERS
public alias 65536 : HITBOX_AUTO
public alias arrayPos6 : currentPlayer
public alias arrayPos7 : playerCount

public alias 10 : water.count

public alias saveRAM[257] : saved.spindash


public alias saveRAM[260] : saved.spikeBehavior
public alias saveRAM[261] : saved.shieldType
public alias saveRAM[262] : saved.Title
public alias saveRAM[263] : saved.Jumplock
public alias saveRAM[264] : saved.Supermusic
public alias saveRAM[265] : saved.Dropdash
public alias saveRAM[266] : saved.Instashield
public alias saveRAM[267] : saved.Peelout
public alias saveRAM[268] : saved.FlightCancel
public alias saveRAM[269] : saved.SonicMaxDash
public alias saveRAM[270] : saved.Speen
public alias saveRAM[271] : saved.Supercancel
public alias saveRAM[272] : saved.Drowning
public alias saveRAM[273] : saved.Timer
public alias saveRAM[274] : saved.region
public alias saveRAM[275] : saved.elemental
public alias saveRAM[276] : saved.rotate
public alias saveRAM[277] : saved.invincibilityType
public alias saveRAM[278] : saved.speedCap
public alias saveRAM[279] : saved.airSpeedCap
public alias saveRAM[280] : saved.GoGoPlus
public alias saveRAM[281] : saved.lives
public alias saveRAM[282] : saved.bubble
public alias saveRAM[283] : saved.timeLimit
public alias saveRAM[285] : saved.randomMonitor
public alias saveRAM[286] : saved.language

public alias saveRAM[294] : saved.graphics
public alias saveRAM[295] : saved.mobile
public alias saveRAM[296] : saved.transmobile
public alias saveRAM[297] : saved.TailsMaxDash
public alias saveRAM[298] : saved.KnuxMaxDash
public alias saveRAM[300] : saved.SuperMaxDash

// Function declarations
reserve function PlayerObject_ProcessPlayer
reserve function PlayerObject_Blank
reserve function PlayerObject_HandleMovement
reserve function PlayerObject_AirAcceleration
reserve function PlayerObject_HandleAirMovement
reserve function PlayerObject_ResetOnFloor
reserve function PlayerObject_StartJump
reserve function PlayerObject_StartSpindash
reserve function PlayerObject_StartTailsFlight
reserve function PlayerObject_StartKnuxGlide
reserve function PlayerObject_HandleGround
reserve function PlayerObject_HandleBored2
reserve function PlayerObject_HandleAir
reserve function PlayerObject_Unknown
reserve function PlayerObject_HandleRolling
reserve function PlayerObject_RollingJump
reserve function PlayerObject_LookingUp
reserve function PlayerObject_Crouching
reserve function PlayerObject_HandleSpindash
reserve function PlayerObject_HandleTailsFlight
reserve function PlayerObject_KnuxGlideLeft
reserve function PlayerObject_KnuxGlideRight
reserve function PlayerObject_KnuxGlideDrop
reserve function PlayerObject_KnuxGlideSlide
reserve function PlayerObject_KnuxWallClimb
reserve function PlayerObject_KnuxLedgePullUp
reserve function PlayerObject_Hurt
reserve function PlayerObject_Knockback
reserve function PlayerObject_Death
reserve function PlayerObject_Drown
reserve function PlayerObject_CheckTile
reserve function PlayerObject_HandleRollDecel
reserve function PlayerObject_Clinging
reserve function PlayerObject_Waterslide
reserve function PlayerObject_HandleFlyCarry
reserve function PlayerObject_HandleContRun
reserve function PlayerObject_InitReplay
reserve function PlayerObject_RestorePowerup
reserve function PlayerObject_ProcessSuperPalS
reserve function PlayerObject_ProcessSuperPalT
reserve function PlayerObject_ProcessSuperPalK
reserve function PlayerObject_SwitchPhysicsState
reserve function PlayerObject_ProcessSuperState
reserve function PlayerObject_CheckHit
reserve function PlayerObject_BadnikBreak
reserve function PlayerObject_Hit
reserve function PlayerObject_FireHit
reserve function PlayerObject_LightningHit
reserve function PlayerObject_ProjectileHit
reserve function PlayerObject_SpikeHit
reserve function PlayerObject_Kill
reserve function PlayerObject_RollAnimSpd
reserve function PlayerObject_WalkAnimSpd
reserve function PlayerObject_RunAnimSpd
reserve function PlayerObject_RollDecel2
reserve function PlayerObject_Transforming
reserve function PlayerObject_SuperTransform
reserve function PlayerObject_BubbleBounce
reserve function PlayerObject_ShieldAbilities
reserve function PlayerObject_StartFlyCarry
reserve function PlayerObject_DropDash
reserve function PlayerObject_HandleSuperPeel
reserve function PlayerObject_StartSuperPeel
reserve function PlayerObject_HandleAirRotate
reserve function PlayerObject_StartSpindashCD
reserve function PlayerObject_HandleSpindashCD
reserve function PlayerObject_ProcessSuper2PalS
reserve function PlayerObject_Force45
reserve function PlayerObject_MoveAnimate
reserve function PlayerObject_ManiaRotate
reserve function PlayerObject_TopGlide
reserve function PlayerObject_EndSuper
reserve function PlayerObject_Funkin

// Static Values
public value PlayerObject_LeaderX = 0;
public value PlayerObject_LeaderY = 0;
public value PlayerObject_SidekickX = 0;
public value PlayerObject_SidekickY = 0;
public value PlayerObject_SuperState = 0;
public value PlayerObject_value1 = 0;
public value PlayerObject_value2 = 0;
public value PlayerObject_value3 = 0;
public value PlayerObject_value4 = 0;
public value PlayerObject_value5 = 0;
public value PlayerObject_value6 = 0;
public value PlayerObject_ReplayTable = 0;
public value PlayerObject_ReplayTablePos = 0;
public value PlayerObject_ReplaySize = 0;
public value PlayerObject_ReplayFrameCnt = 0;
public value PlayerObject_ReplayLength = 0;
private value value13 = 0;
private value value14 = 0;
private value value15 = 0;
private value value16 = 0;
private value value17 = 0;
private value value18 = 0;
private value value19 = 0;
public value achievement.trigger = 0;

// Tables
public table PlayerObject_SonicSuperPal
	0x2020A0, 0x2040C0, 0x4040E0, 0x6060E0
	0x404080, 0x4060A0, 0x6060E0, 0x8080E0
	0x606060, 0x6080A0, 0x8080E0, 0xA0A0E0
	0x808040, 0x80A0A0, 0xA0A0E0, 0xC0C0E0
	0xA0A040, 0xA0C0A0, 0xC0C0E0, 0xE0E0E0
	0xC0C040, 0xC0E0A0, 0xE0E0E0, 0xE0E0E0
	0xE0E040, 0xE0E0A0, 0xE0E0E0, 0xE0E0E0
	0xE0E060, 0xE0E0E0, 0xE0E0E0, 0xE0E0E0
	0xE0E080, 0xE0E0E0, 0xE0E0E0, 0xE0E0E0
	0xE0E060, 0xE0E0C0, 0xE0E0E0, 0xE0E0E0
	0xE0E040, 0xE0E0A0, 0xE0E0E0, 0xE0E0E0
	0xE0E020, 0xE0E080, 0xE0E0C0, 0xE0E0E0
	0xE0E000, 0xE0E060, 0xE0E0A0, 0xE0E0E0
	0xE0E000, 0xE0E040, 0xE0E080, 0xE0E0C0
	0xE0E000, 0xE0E060, 0xE0E0A0, 0xE0E0E0
	0xE0E000, 0xE0E080, 0xE0E0C0, 0xE0E0E0
end table

public table PlayerObject_SonicSuperAltPal
	0x202080, 0x4040A0, 0x6060C0, 0x8080E0
	0x404060, 0x6060A0, 0x8080E0, 0xA0A0E0
	0x606040, 0x8080A0, 0xA0A0E0, 0xC0C0E0
	0x808040, 0xA0A0A0, 0xC0C0E0, 0xE0E0E0
	0xA0A040, 0xC0C0A0, 0xE0E0E0, 0xE0E0E0
	0xC0C040, 0xE0E0A0, 0xE0E0E0, 0xE0E0E0
	0xE0E040, 0xE0E0A0, 0xE0E0E0, 0xE0E0E0
	0xE0E060, 0xE0E0E0, 0xE0E0E0, 0xE0E0E0
	0xE0E080, 0xE0E0E0, 0xE0E0E0, 0xE0E0E0
	0xE0E060, 0xE0E0C0, 0xE0E0E0, 0xE0E0E0
	0xE0E040, 0xE0E0A0, 0xE0E0E0, 0xE0E0E0
	0xE0E020, 0xE0E080, 0xE0E0C0, 0xE0E0E0
	0xE0E000, 0xE0E060, 0xE0E0A0, 0xE0E0E0
	0xE0E000, 0xE0E040, 0xE0E080, 0xE0E0C0
	0xE0E000, 0xE0E060, 0xE0E0A0, 0xE0E0E0
	0xE0E000, 0xE0E080, 0xE0E0C0, 0xE0E0E0
end table

public table PlayerObject_SonicSuper2Pal
	0x2020A0, 0x2040C0, 0x4040E0, 0x6060E0
	0x404080, 0x4060A0, 0x6060E0, 0x8080E0
	0x606060, 0x6080A0, 0x8080E0, 0xA0A0E0
	0x808080, 0x80A0A0, 0xA0A0E0, 0xC0C0E0
	0xA0A0A0, 0xC0C0C0, 0xC0C0E0, 0xE0E0E0
	0xC0C0C0, 0xE0E0E0, 0xE0E0E0, 0xE0E0E0
	
	0xE0E0E0, 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 0xA0E0A0, 0xC0E0C0, 0xC0E0C0
	0xB0E0B0, 0xC0E0C0, 0xD0E0D0, 0xD0E0D0
end table

public table PlayerObject_SonicSuper3Pal
	0x60A020, 0xA02000, 0x000000, 0x000000
	0x60A020, 0xC02000, 0x000000, 0x000000
	0x60A020, 0xE02000, 0x000000, 0x000000
	0x60A020, 0xE04000, 0x000000, 0x000000
	0x60A020, 0x202000, 0x000000, 0x000000
	0x60A020, 0x404000, 0x000000, 0x000000
	0x60A020, 0x80E000, 0x000000, 0x000000
	0x60A020, 0xC0E000, 0x000000, 0x000000
end table

public table PlayerObject_TailsSuperPal
	0x800000, 0xE08000, 0xE0A000, 0xA06040
	0x800000, 0xE0A020, 0xE0C040, 0xA06040
	0xA06040, 0xE0C040, 0xE0C080, 0xC06040
	0xC06040, 0xE0C080, 0xE0E0A0, 0xE08040
	0xA06040, 0xE0C040, 0xE0C080, 0xC06040
	0x800000, 0xE0A020, 0xE0C040, 0xA06040
end table

public table PlayerObject_TailsSuperAltPal
	0x800000, 0xE08000, 0xE0A000, 0xA06040
	0x800000, 0xE0A020, 0xE0C040, 0xA06040
	0xA06040, 0xE0C040, 0xE0C080, 0xC06040
	0xC06040, 0xE0C080, 0xE0E0A0, 0xE08040
	0xA06040, 0xE0C040, 0xE0C080, 0xC06040
	0x800000, 0xE0A020, 0xE0C040, 0xA06040
end table

public table PlayerObject_KnuxSuperPal
	0x600020, 0xC00020, 0xE04060
	0x802040, 0xE04060, 0xE060A0
	0xA04060, 0xE06080, 0xE080C0
	0xC06080, 0xE080A0, 0xE0A0E0
	0xE080A0, 0xE0A0C0, 0xE0C0E0
	0xE0A0C0, 0xE0C0E0, 0xE0E0E0
	0xE080A0, 0xE0A0C0, 0xE0C0E0
	0xC06080, 0xE080A0, 0xE0A0E0
	0xA04060, 0xE06080, 0xE080C0
	0x802040, 0xE04060, 0xE060A0
end table

public table PlayerObject_KnuxSuperAltPal
	0x600020, 0xC00040, 0xE04080
	0x802040, 0xE04060, 0xE060A0
	0xA04060, 0xE06080, 0xE080C0
	0xC06080, 0xE080A0, 0xE0A0E0
	0xE080A0, 0xE0A0C0, 0xE0C0E0
	0xE0A0C0, 0xE0C0E0, 0xE0E0E0
	0xE080A0, 0xE0A0C0, 0xE0C0E0
	0xC06080, 0xE080A0, 0xE0A0E0
	0xA04060, 0xE06080, 0xE080C0
	0x802040, 0xE04060, 0xE060A0
end table

public table PlayerObject_SonicPhysicsTable
	0x60000, 0xC00,  0x1800, 0x600,  0x8000,  0x600, 0x68000, -0x40000
	0x30000, 0x600,  0xC00,  0x300,  0x4000,  0x300, 0x38000, -0x20000
	0xA0000, 0x3000, 0x6000, 0x1800, 0x10000, 0x600, 0x80000, -0x40000
	0x50000, 0x1800, 0x3000, 0xC00,  0x8000,  0x300, 0x38000, -0x20000
	0xC0000, 0x1800, 0x3000, 0xC00,  0x8000,  0x600, 0x68000, -0x40000
	0x60000, 0xC00,  0x1800, 0x600,  0x4000,  0x300, 0x38000, -0x20000
	0xC0000, 0x1800, 0x3000, 0xC00,  0x8000,  0x600, 0x80000, -0x40000
	0x60000, 0xC00,  0x1800, 0x600,  0x4000,  0x300, 0x38000, -0x20000
end table

public table PlayerObject_TailsPhysicsTable
	0x60000, 0xC00,  0x1800, 0x600,  0x8000,  0x600, 0x68000, -0x40000
	0x30000, 0x600,  0xC00,  0x300,  0x4000,  0x300, 0x38000, -0x20000
	0xA0000, 0x3000, 0x6000, 0x1800, 0x10000, 0x600, 0x80000, -0x40000
	0x50000, 0x1800, 0x3000, 0xC00,  0x8000,  0x300, 0x38000, -0x20000
	0xC0000, 0x1800, 0x3000, 0xC00,  0x8000,  0x600, 0x68000, -0x40000
	0x60000, 0xC00,  0x1800, 0x600,  0x4000,  0x300, 0x38000, -0x20000
	0xC0000, 0x1800, 0x3000, 0xC00,  0x8000,  0x600, 0x80000, -0x40000
	0x60000, 0xC00,  0x1800, 0x600,  0x4000,  0x300, 0x38000, -0x20000
end table

public table PlayerObject_KnuxPhysicsTable
	0x60000, 0xC00,  0x1800, 0x600,  0x8000,  0x600, 0x60000, -0x40000
	0x30000, 0x600,  0xC00,  0x300,  0x4000,  0x300, 0x30000, -0x20000
	0xA0000, 0x3000, 0x6000, 0x1800, 0x10000, 0x600, 0x60000, -0x40000
	0x50000, 0x1800, 0x3000, 0xC00,  0x8000,  0x300, 0x30000, -0x20000
	0xC0000, 0x1800, 0x3000, 0xC00,  0x8000,  0x600, 0x60000, -0x40000
	0x60000, 0xC00,  0x1800, 0x600,  0x4000,  0x300, 0x30000, -0x20000
	0xC0000, 0x1800, 0x3000, 0xC00,  0x8000,  0x600, 0x60000, -0x40000
	0x60000, 0xC00,  0x1800, 0x600,  0x8000,  0x300, 0x30000, -0x20000
end table

function PlayerObject_InitReplay
	PlayerObject_ReplayTablePos = 2
	PlayerObject_ReplayFrameCnt = 1
	currentPlayer = 0
	while currentPlayer < playerCount
		GetTableValue(object[currentPlayer].xpos, 0, PlayerObject_ReplayTable)
		GetTableValue(object[currentPlayer].ypos, 1, PlayerObject_ReplayTable)
		object[currentPlayer].controlMode = -1
		object[currentPlayer].up = 0
		object[currentPlayer].down = 0
		object[currentPlayer].left = 0
		object[currentPlayer].right = 0
		object[currentPlayer].jumpPress = 0
		object[currentPlayer].jumpHold = 0
		object[currentPlayer].value1 = 0
		currentPlayer++
	loop
	screen.cameraX = object[0].ixpos
	screen.cameraY = object[0].iypos
end function


function PlayerObject_RestorePowerup
	switch object[currentPlayer].value37
	case 0
		ResetObjectEntity(arrayPos0, TypeName[Blank Object], 0, 0, 0)
		break
	case 1
		ResetObjectEntity(arrayPos0, blueShieldType, 0, 0, 0)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		object[arrayPos0].inkEffect = INK_ALPHA
		object[arrayPos0].alpha = 160
		break
	case 2
		ResetObjectEntity(arrayPos0, TypeName[Bubble Shield], 0, 0, 0)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		break
	case 3
		ResetObjectEntity(arrayPos0, TypeName[Fire Shield], 0, 0, 0)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		break
	case 4
		ResetObjectEntity(arrayPos0, TypeName[LightningShield], 0, 0, 0)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		break
	end switch
end function

function PlayerObject_ProcessSuperPalS
	if saveRAM[482] == 1
		CallFunction(PlayerObject_ProcessSuper2PalS)
	else
		if PlayerObject_SuperState == 1
			PlayerObject_value3++
			if PlayerObject_value3 >= 4
				PlayerObject_value3 = 0
				PlayerObject_value2 += 4
				if PlayerObject_value2 >= 64
					PlayerObject_value2 = 24
				end if
			end if
		else
			PlayerObject_value3++
			if PlayerObject_value3 >= 8
				PlayerObject_value3 = 0
				PlayerObject_value2 -= 4
				if PlayerObject_value2 <= 0
					PlayerObject_value2 = 0
					PlayerObject_SuperState = 0
				end if
				if PlayerObject_value2 >= 24
					PlayerObject_value2 = 24
				end if
			end if
		end if
		temp1 = PlayerObject_value2
		GetTableValue(temp0, temp1, PlayerObject_SonicSuperPal)
		SetPaletteEntry(0, 2, temp0)
		GetTableValue(temp0, temp1, PlayerObject_SonicSuperAltPal)
		SetPaletteEntry(1, 2, temp0)
		temp1++
		GetTableValue(temp0, temp1, PlayerObject_SonicSuperPal)
		SetPaletteEntry(0, 3, temp0)
		GetTableValue(temp0, temp1, PlayerObject_SonicSuperAltPal)
		SetPaletteEntry(1, 3, temp0)
		temp1++
		GetTableValue(temp0, temp1, PlayerObject_SonicSuperPal)
		SetPaletteEntry(0, 4, temp0)
		GetTableValue(temp0, temp1, PlayerObject_SonicSuperAltPal)
		SetPaletteEntry(1, 4, temp0)
		temp1++
		GetTableValue(temp0, temp1, PlayerObject_SonicSuperPal)
		SetPaletteEntry(0, 5, temp0)
		GetTableValue(temp0, temp1, PlayerObject_SonicSuperAltPal)
		SetPaletteEntry(1, 5, temp0)
	end if
end function

function PlayerObject_ProcessSuper2PalS
	if PlayerObject_SuperState == 1
		PlayerObject_value3++
		if PlayerObject_value3 >= 6
			PlayerObject_value3 = 0
			PlayerObject_value2 += 4
			if PlayerObject_value2 >= 120
				PlayerObject_value2 = 24
			end if
		end if
	else
		PlayerObject_value3++
		if PlayerObject_value3 >= 8
			PlayerObject_value3 = 0
			PlayerObject_value2 -= 4
			if PlayerObject_value2 <= 0
				PlayerObject_value2 = 0
				PlayerObject_SuperState = 0
			end if
			if PlayerObject_value2 >= 24
				PlayerObject_value2 = 24
			end if
		end if
	end if
	temp1 = PlayerObject_value2
	GetTableValue(temp0, temp1, PlayerObject_SonicSuper2Pal)
	SetPaletteEntry(0, 2, temp0)
	GetTableValue(temp0, temp1, PlayerObject_SonicSuper2AltPal)
	SetPaletteEntry(1, 2, temp0)
	temp1++
	GetTableValue(temp0, temp1, PlayerObject_SonicSuper2Pal)
	SetPaletteEntry(0, 3, temp0)
	GetTableValue(temp0, temp1, PlayerObject_SonicSuper2AltPal)
	SetPaletteEntry(1, 3, temp0)
	temp1++
	GetTableValue(temp0, temp1, PlayerObject_SonicSuper2Pal)
	SetPaletteEntry(0, 4, temp0)
	GetTableValue(temp0, temp1, PlayerObject_SonicSuper2AltPal)
	SetPaletteEntry(1, 4, temp0)
	temp1++
	GetTableValue(temp0, temp1, PlayerObject_SonicSuper2Pal)
	SetPaletteEntry(0, 5, temp0)
	GetTableValue(temp0, temp1, PlayerObject_SonicSuper2AltPal)
	SetPaletteEntry(1, 5, temp0)
end function


function PlayerObject_ProcessSuperPalT
	if PlayerObject_SuperState == 1
		PlayerObject_value3++
		if PlayerObject_value3 >= 12
			PlayerObject_value3 = 0
			PlayerObject_value2 += 4
			if PlayerObject_value2 >= 24
				PlayerObject_value2 = 0
			end if
		end if
	else
		if PlayerObject_value2 > 12
			FlipSign(PlayerObject_value2)
			PlayerObject_value2 += 24
		end if
		PlayerObject_value3++
		if PlayerObject_value3 >= 12
			PlayerObject_value3 = 0
			PlayerObject_value2 -= 4
			if PlayerObject_value2 <= 0
				PlayerObject_value2 = 0
				PlayerObject_SuperState = 0
			end if
		end if
	end if
	temp1 = PlayerObject_value2
	GetTableValue(temp0, temp1, PlayerObject_TailsSuperPal)
	SetPaletteEntry(0, 13, temp0)
	GetTableValue(temp0, temp1, PlayerObject_TailsSuperAltPal)
	SetPaletteEntry(1, 13, temp0)
	temp1++
	GetTableValue(temp0, temp1, PlayerObject_TailsSuperPal)
	SetPaletteEntry(0, 17, temp0)
	GetTableValue(temp0, temp1, PlayerObject_TailsSuperAltPal)
	SetPaletteEntry(1, 17, temp0)
	temp1++
	GetTableValue(temp0, temp1, PlayerObject_TailsSuperPal)
	SetPaletteEntry(0, 18, temp0)
	GetTableValue(temp0, temp1, PlayerObject_TailsSuperAltPal)
	SetPaletteEntry(1, 18, temp0)
	temp1++
	GetTableValue(temp0, temp1, PlayerObject_TailsSuperPal)
	SetPaletteEntry(0, 16, temp0)
	GetTableValue(temp0, temp1, PlayerObject_TailsSuperAltPal)
	SetPaletteEntry(1, 16, temp0)
end function


function PlayerObject_ProcessSuperPalK
	if PlayerObject_SuperState == 1
		PlayerObject_value3++
		if PlayerObject_value3 >= 12
			PlayerObject_value3 = 9
			PlayerObject_value2 += 3
			if PlayerObject_value2 >= 30
				PlayerObject_value3 = 0
				PlayerObject_value2 = 0
			end if
		end if
	else
		if PlayerObject_value2 > 15
			FlipSign(PlayerObject_value2)
			PlayerObject_value2 += 30
		end if
		PlayerObject_value3++
		if PlayerObject_value3 >= 12
			PlayerObject_value3 = 0
			PlayerObject_value2 -= 3
			if PlayerObject_value2 <= 0
				PlayerObject_value2 = 0
				PlayerObject_SuperState = 0
			end if
		end if
	end if
	temp1 = PlayerObject_value2
	GetTableValue(temp0, temp1, PlayerObject_KnuxSuperPal)
	SetPaletteEntry(0, 26, temp0)
	GetTableValue(temp0, temp1, PlayerObject_KnuxSuperAltPal)
	SetPaletteEntry(1, 26, temp0)
	temp1++
	GetTableValue(temp0, temp1, PlayerObject_KnuxSuperPal)
	SetPaletteEntry(0, 27, temp0)
	GetTableValue(temp0, temp1, PlayerObject_KnuxSuperAltPal)
	SetPaletteEntry(1, 27, temp0)
	temp1++
	GetTableValue(temp0, temp1, PlayerObject_KnuxSuperPal)
	SetPaletteEntry(0, 28, temp0)
	GetTableValue(temp0, temp1, PlayerObject_KnuxSuperAltPal)
	SetPaletteEntry(1, 28, temp0)
end function


function PlayerObject_SwitchPhysicsState
	switch stage.playerListPos
	case 0
	case 3
		temp0 = PlayerObject_SonicPhysicsTable
		break
	case 1
		temp0 = PlayerObject_TailsPhysicsTable
		break
	case 2
		temp0 = PlayerObject_KnuxPhysicsTable
		break
	end switch
	temp1 = 0
	temp2 = 0
	if stage.state != 3
		temp3 = object[currentPlayer].ypos
		temp3 >>= 16
		CheckGreater(temp3, stage.waterLevel)
		temp4 = checkResult
		CheckNotEqual(object[currentPlayer].type, TypeName[Debug Mode])
		temp4 &= checkResult
		if temp4 == 1
			SetBit(temp1, 0, 1)
			object[currentPlayer].value25 = 0x1000
		else
			object[currentPlayer].value25 = 0x3800
		end if
		if PlayerObject_SuperState == 1
			SetBit(temp1, 1, 1)
			temp2 = 2
		end if
		if object[currentPlayer].value6 > 0
			SetBit(temp1, 2, 1)
			temp2 = 1
		end if
		temp1 <<= 3
	end if
	GetTableValue(object[currentPlayer].value20, temp1, temp0)
	temp1++
	GetTableValue(object[currentPlayer].value21, temp1, temp0)
	object[currentPlayer].value22 = object[currentPlayer].value21
	object[currentPlayer].value22 >>= temp2
	temp1++
	GetTableValue(object[currentPlayer].value23, temp1, temp0)
	temp1++
	//GetTableValue(object[currentPlayer].value24, temp1, temp0)
	temp1++
	GetTableValue(object[currentPlayer].value9, temp1, temp0)
	temp1++
	GetTableValue(object[currentPlayer].value29, temp1, temp0)
	temp1++
	GetTableValue(object[currentPlayer].value27, temp1, temp0)
	temp1++
	GetTableValue(object[currentPlayer].value28, temp1, temp0)
end function


function PlayerObject_ProcessSuperState
	if PlayerObject_SuperState != 0
		switch stage.playerListPos
		case 0
		case 3
			CallFunction(PlayerObject_ProcessSuperPalS)
			break
		case 1
			CallFunction(PlayerObject_ProcessSuperPalT)
			break
		case 2
			CallFunction(PlayerObject_ProcessSuperPalK)
			break
		end switch
	end if
	if PlayerObject_SuperState == 1
		object.value7 = 60
		PlayerObject_value1++
		if PlayerObject_value1 == 60
			PlayerObject_value1 = 0
			object.value0--
			if object.value0 <= 0
				object.value0 = 0
				PlayerObject_SuperState = 2
			end if
		end if
	end if
	if PlayerObject_SuperState == 2
		CallFunction(PlayerObject_EndSuper)
	end if
end function

function PlayerObject_EndSuper
	if stage.playerListPos == 0
		LoadAnimation("Sonic.ani")
	end if
	if music.currentTrack == 7
		PlayMusic(0)
	end if
	if saved.Supermusic == 5
		if object.value6 < 1
			CallFunction(SlowDownMusic)
		end if
	end if
	object.value7 = 0
	currentPlayer = object.entityPos
	if object.state != PlayerObject_Death
		if object.state != PlayerObject_Drown
			arrayPos0 = currentPlayer
			arrayPos0 += playerCount
			CallFunction(PlayerObject_RestorePowerup)
			object.value8 = 60
		end if
	end if
	PlayerObject_SuperState = 3
	CallFunction(PlayerObject_SwitchPhysicsState)
	if SuperTimeAttack == 1
		if stage.timeEnabled == 1
			ResetObjectEntity(11, TypeName[Title Card], 0, 0, 0)
			object[11].state = 10
			object[11].priority = PRIORITY_ACTIVE
			object[11].drawOrder = 6
			object.value7 = 80
			screen.cameraEnabled = 0
		end if
	end if
end function

function PlayerObject_Funkin
		object.state = PlayerObject_Funkin
		object.animation = ANI_STOPPED
		object.direction = 1
		
		if inputdown[0].right == 1
			object.animationSpeed = 0
			object.animationTimer = 0
			object.animation = ANI_SKIDDING
		end if

		if inputdown[0].left == 1
			object.animationSpeed = 0
			object.animationTimer = 0

			object.animation = ANI_FLAILING1
		end if

		if inputdown[0].up == 1
			object.animationSpeed = 0
			object.animationTimer = 0

			object.frame = 2

			object.animation = ANI_LOOKINGUP
		end if

		if inputdown[0].down == 1
			object.animationSpeed = 0
			object.animationTimer = 0

			object.frame = 2

			object.animation = ANI_LOOKINGDOWN
		end if

		if inputpress[0].buttonX == 1
			screen.cameraStyle = 0
			object.speed = 0
			object.yvel = -0x70000
			object.xvel = 0
			PlaySfx(SfxName[Hurt], 0)	
			object.state = PlayerObject_Death
			object.animation = ANI_DYING
			object.tileCollisions = 0
			object.interaction = 0
			if object.entityPos == screen.cameraTarget
				object.priority = PRIORITY_ACTIVE_PAUSED
				if object[1].type == TypeName[Player 2 Object]
					object[1].priority = PRIORITY_ACTIVE_PAUSED
				end if
				screen.cameraEnabled = 0
				if options.vsMode == 0
					stage.state = 3		
					stage.pauseEnabled = 0
				end if
			end if
		end if
end function



event ObjectMain
	if SuperTimeAttack == 2
		SuperTimeAttack = 1
		currentPlayer = 0
		CallFunction(PlayerObject_SuperTransform)
	end if
	CallFunction(PlayerObject_ProcessPlayer)
	if object.gravity == GRAVITY_GROUND
		object.value35 = 0
	end if
	CallFunction(object.state)
	ProcessAnimation()
	if object.entityPos == screen.cameraTarget
		if object.animation == ANI_JUMPING
			screen.adjustCameraY = object.value30
		else
			if screen.adjustCameraY == object.value30
				screen.adjustCameraY = 0
				object.iypos += object.value30
			end if
		end if
	end if
	if object.value34 == 0
		temp0 = object.value42
		object.value42 = object.gravity
		ProcessObjectMovement()
		object.value42 ^= 1
		CheckEqual(object.gravity, GRAVITY_GROUND)
		object.value42 |= checkResult
		object.value42 ^= 1
		if temp0 == 1
			if object.value42 == 0
				object.value19 = 0
				if object.animation == ANI_JUMPING
					if object.down == 0
						if object.state != PlayerObject_BubbleBounce
							if object.state != PlayerObject_HandleRolling
								if object.state != PlayerObject_HandleRollDecel
									object.animation = ANI_WALKING
									if object.entityPos == screen.cameraTarget
										screen.adjustCameraY = 0
									end if
									object.iypos += object.value30
								end if
							end if
						end if
					end if
				end if
			end if
		end if
	else
		object.value34 = 0
	end if
		
	if inputPress.buttonY == 1
		if options.gameMode == 2
			if stage.timeEnabled == 1
				stage.timeEnabled = 0
				stage.pauseEnabled = 0
				ResetObjectEntity(11, TypeName[Title Card], 0, 0, 0)
				object[11].state = 10
				object[11].priority = PRIORITY_ACTIVE
				object[11].drawOrder = 6
				object.value7 = 80
				screen.cameraEnabled = 0			
			end if
		else

		if stage.activeList != BONUS_STAGE
					if PlayerObject_SuperState == 1
						if object.animation != ANI_SUPER_TRANSFORM
							if saved.Supercancel == 1
								CheckCurrentStageFolder("Ending")
								if checkResult == 0
									CallFunction(PlayerObject_EndSuper)
								end if
							end if
						end if
					end if
				end if
		end if
	end if		
end event


event ObjectDraw
		if object.value15 >= 21
			CheckEqual(object.animation,ANI_JUMPING)
			temp0 = checkResult
			CheckEqual(object.animation,ANI_WALKING)
			temp0 |= checkResult
			CheckEqual(object.animation,ANI_STOPPED)
			temp0 |= checkResult
			if temp0 == 0
				object.value15 = 0
			end if
		else
			temp0 = 0
		end if

		if temp0 == 1
			temp1 = object.value15
			temp1 %= 8
		
			DrawSpriteFX(temp1, FX_FLIP, object.xpos, object.ypos)
		else
			if object.animation != object.prevAnimation
				object.prevAnimation = object.animation
				object.frame = 0
				object.animationTimer = 0
				object.animationSpeed = 0
			end if
			
			if saved.rotate == 0
				CallFunction(PlayerObject_Force45)
			else
				CallFunction(PlayerObject_ManiaRotate)
				//DrawObjectAnimation()
			end if
		end if
end event



event ObjectStartup
	playerCount = 0
	foreach (TypeName[Funky Player], arrayPos0, ALL_ENTITIES)
		screen.cameraEnabled = 1
		screen.cameraStyle = 0
		screen.cameraTarget = 0
		currentPlayer = 0
		switch stage.playerListPos
			case 3
				stage.playerListPos = 0
				stage.player2Enabled = 1
				break
			case 4
				stage.playerListPos = 2
				stage.player2Enabled = 1
				break
			case 100
				stage.playerListPos = 0
				SuperTimeAttack = 1
				break
		end switch
		ResetObjectEntity(0, TypeName[Funky Player], 0, object[arrayPos0].xpos, object[arrayPos0].ypos)
		screen.cameraX = object[0].ixpos
		screen.cameraY = object[0].iypos
		object[0].groupID = GROUP_PLAYERS
		object[0].state = PlayerObject_Funkin
		screen.cameraStyle = 3
		object[0].priority = PRIORITY_ACTIVE
		object[0].drawOrder = -1
		object[0].value18 = 4
		object[0].value31 = 0x2000

		if SuperTimeAttack == 1 //Super Sonic Time Attack
			object[0].value0 = 20
		end if
		
		switch saved.spindash
  		case 0
			object[0].value33 = PlayerObject_StartSpindash
			break
		case 1
			object[0].value33 = PlayerObject_StartSpindashCD
			break
   		case 2
			if options.attractMode == 0
				object[0].value33 = PlayerObject_StartJump
			else
				object[0].value33 = PlayerObject_StartSpindash
			end if
    			break
		end switch		
		object[0].value38 = HITBOX_AUTO
		object[0].value39 = HITBOX_AUTO
		object[0].value40 = HITBOX_AUTO
		object[0].value41 = HITBOX_AUTO
		PlayerObject_SuperState = 0
		PlayerObject_value1 = 0
		PlayerObject_value2 = 0
		PlayerObject_value3 = 0
		switch stage.playerListPos
		case 0
			LoadAnimation("SuperSonic.ani")
			LoadAnimation("Sonic.ani")
			object[0].propertyValue = 0
			object[0].value30 = -5
			object[0].value32 = PlayerObject_ShieldAbilities
			break
		case 1
			object[0].type = TypeName[Tails Object]
			object[0].propertyValue = 1
			CallFunction(PlayerObject_ProcessSuperPalT)
			LoadAnimation("Tails.ani")
			object[0].value30 = -1
			object[0].value32 = PlayerObject_StartTailsFlight
			stage.player2Enabled = 0
			break
		case 2
			LoadAnimation("Knuckles.ani")
			CallFunction(PlayerObject_ProcessSuperPalK)
			object[0].propertyValue = 2
			object[0].value30 = -5
			object[0].value32 = PlayerObject_StartKnuxGlide
			//if options.saveSlot > 0
			//	stage.player2Enabled = 0
			//end if
			break
		end switch
		
		//if saved.Speen == 1
		//	ANI_BOUNCING = ANI_SPEEN
		//else
		//	ANI_BOUNCING = 11
		//end if 
		
		if stage.player2Enabled == 1
			playerCount = 2
		else
			playerCount = 1
		end if
		currentPlayer = 0
		CallFunction(PlayerObject_SwitchPhysicsState)
		ResetObjectEntity(arrayPos0, TypeName[Blank Object], 0, 0, 0)
	next
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
